using System;

namespace Evaluant.OPath.Expressions
{
	[Serializable]
    public class Function : Expression, ICloneable
	{
		/// <summary>
		/// Creates a new <see cref="Function"/> instance.
		/// </summary>
		public Function() : base()
		{
		}

        public Function(Path path, FunctionEnum type)
        {
            this.path = path;
            this.type = type;
        }

        [System.Diagnostics.DebuggerStepThrough]
        public override void Accept(OPathVisitor visitor)
		{
			visitor.Visit(this);
		}

		private Path path;
		public Path Path
		{
            get { return path; }
            set { path = value; }
		}

		private FunctionEnum type;
		/// <summary>
		/// Gets or sets the type.
		/// </summary>
		/// <value></value>
		public FunctionEnum Type
		{
			get { return type; }
			set { type = value; }
		}

        public override void ToString(System.Text.StringBuilder sb)
        {

            switch (type)
            {
                case FunctionEnum.Max:
                    sb.Append("max(");
                    break;
                case FunctionEnum.Min:
                    sb.Append("min(");
                    break;
                case FunctionEnum.Count:
                    sb.Append("count(");
                    break;
                case FunctionEnum.Average:
                    sb.Append("avg(");
                    break;
                case FunctionEnum.Sum:
                    sb.Append("sum(");
                    break;
                case FunctionEnum.Exists:
                    sb.Append("exists(");
                    break;
                case FunctionEnum.IsNull:
                    sb.Append("isNull(");
                    break;
            }

            path.ToString(sb);

            sb.Append(")");
        }

		#region Membres de ICloneable

		public override object Clone()
		{
			Function function = new Function();
			function.Type = this.Type;
			function.Path = this.Path == null ? null : (Path)this.Path.Clone();

			return function;
		}

		#endregion
	}

    

	public enum FunctionEnum
	{
		Unknown = 0,
		Max = 1, 
		Min = 2, 
		Count = 3, 
		Average = 4, 
		Sum = 5, 
		Exists = 6, 
		IsNull = 7,
	}
}
